#pragma once
#include "../TcpServer.hpp"
class EchoServer
{
public:
    EchoServer(int port)
        :_server(port)
    {
        _server.SetThreadCount(2);
        _server.EnableInactiveReleanse(10);
        _server.SetClosedCallBack(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        _server.SetConnectedCallBack(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _server.SetMessageCallBack(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }
    void Start() { _server.Start(); }
private:
    void OnClosed(const PtrConnection& conn)//连接关闭 
    {
        DEBUG_LOG("CLOSE CONNECTION:%p", conn.get());
    }
    void OnConnected(const PtrConnection& conn)//新连接建立成功
    {
        DEBUG_LOG("NEW CONNECTION:%p", conn.get());
    }
    void OnMessage(const PtrConnection& conn, Buffer* buffer)//消息来了(收到的数据再发送出去)
    {
        conn->Send(buffer->ReadPosition(), buffer->ReadAbleSize());//发送数据，将数据放到发送缓冲区，启动写事件监控
        buffer->MoveReadOffset(buffer->ReadAbleSize());//将读偏移向后移动
        conn->Shutdown(); //提供给组件使用者的关闭接口,并不是立即关闭（先判断有没有数据待处理）
    }
private:
    TcpServer _server;
};